package com.hibiscusking.org.controller;

import com.hibiscusking.basic.config.BussinessException;
import com.hibiscusking.basic.util.AjaxResult;
import com.hibiscusking.basic.util.ExcelUtil;
import com.hibiscusking.basic.util.PageList;
import com.hibiscusking.org.domain.Shop;
import com.hibiscusking.org.domain.ShopAuditLog;
import com.hibiscusking.org.query.ShopQuery;
import com.hibiscusking.org.service.ShopAuditLogService;
import com.hibiscusking.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopAuditLogService shopAuditLogService;
    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.dele(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }
    @GetMapping("/newSettle/{id}")
    public Shop getSettle(@PathVariable("id")Long id)
    {
        return shopService.findShopAndEmpById(id);
    }

    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping()
    public List list(){

        return shopService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        PageList<Shop> shopPageList = shopService.queryByPage(query);
        return shopPageList;
    }

    @PostMapping("/settlement")
    public AjaxResult settleIn(@RequestBody Shop shop) {
        shopService.settleIn(shop);
        return AjaxResult.me().success();
    }
    @PostMapping("/audit/reject")
    public AjaxResult examine(@RequestBody ShopAuditLog shopAuditLog) {
        if (shopAuditLog.getNote() == null || "".equals(shopAuditLog.getNote().trim())) {
            throw new BussinessException("备注不能为空");
        }
        System.out.println(shopAuditLog.getType() == 1);
        if (shopAuditLog.getType() == 1) { // 通过
            shopService.approved(shopAuditLog);
        } else { // 驳回
            shopService.reject(shopAuditLog);
        }
        return AjaxResult.me().success();
    }
    @GetMapping("/audit/{id}")
    public AjaxResult modifyState(@PathVariable("id") Long id) {
        shopService.modifyState(id);

        return AjaxResult.me().success();
    }
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response) {
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops,null,"sheet1",Shop.class,"店铺信息.xlsx",response);
    }
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile multipartFile) {
        List<Shop> shops = ExcelUtil.importExcel(multipartFile, 0, 1, Shop.class);
        shopService.addShops(shops);
    }
    /**
     * 报表
     * */
    @GetMapping("/echarts")
    public Map<String,Object> shopReportForm() {
        return shopService.shopReportForm();
    }
}
